package com.esun.payment_api.service.impl;

import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.DkContractBean;
import com.esun.payment_api.domain.SysCustomerExample;
import com.esun.payment_api.domain.SysDictContract;
import com.esun.payment_api.domain.SysDictContractExample;
import com.esun.payment_api.mapper.SysDictContractMapper;
import com.esun.payment_api.security.AjaxResult;
import com.esun.payment_api.service.SysDictContractService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("SysDictContractService")
public class SysDictContractServiceImpl implements SysDictContractService {

    @Resource
    SysDictContractMapper sysDictContractMapper;

    @Override
    public List<SysDictContract> selectByExample(Short coId, String keyword, String word) {
        PageHelper.startPage(1, 10);
        //创建Example对象
        SysDictContractExample example = new SysDictContractExample();
        //创建criteria
        SysDictContractExample.Criteria criteria = example.createCriteria();
        //where条件
        criteria.andIsDeletedEqualTo("0");
        if (!BlankUtils.isBlank(coId)) {
            criteria.andCoIdEqualTo(coId);
        }
        if (!BlankUtils.isBlank(keyword) && !BlankUtils.isBlank(word)) {
            switch (keyword) {
                case "code":
                    criteria.andCodeLike("%" + word + "%");
                    break;
                case "name":
                    criteria.andNameLike("%" + word + "%");
                    break;
                default:
                    System.out.println("输入参数有误！");
                    break;
            }
        }
        return sysDictContractMapper.selectByExample(example);
    }

    @Override
    public int insertSelective(SysDictContract record) {
        return sysDictContractMapper.insertSelective(record);
    }

    @Override
    public AjaxResult deleteById(Integer id) {
        try {
            if (sysDictContractMapper.deleteById(id) == 1) {
                return AjaxResult.success("删除成功");
            } else {
                return AjaxResult.error("删除失败");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult updateByPrimaryKeySelective(SysDictContract record) {
        try {
            if (sysDictContractMapper.updateByPrimaryKeySelective(record) == 1) {
                return AjaxResult.success("修改成功");
            } else {
                return AjaxResult.error("修改失败");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public List<DkContractBean> claimSelectContract(Short companyId, Integer claimId, Integer customerId, String searchStr, Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        return sysDictContractMapper.claimSelectContract(companyId,claimId,customerId,searchStr);
    }

    @Override
    public List<SysDictContract> getSysDictContractByCode(String code) {
        SysDictContractExample example = new SysDictContractExample();
        SysDictContractExample.Criteria criteria = example.createCriteria();
        criteria.andCodeEqualTo(code);
        return sysDictContractMapper.selectByExample(example);
    }
}
